//静态方法，处理材质
var cacheIdx = 0;
var nameEx = "AnimationLine";
class LineFlowMaterial {
	constructor(options) {
		console.log(options)
		options = Cesium.defaultValue(options, Cesium.defaultValue.EMPTY_OBJECT);
		this.defaultColor = new Cesium.Color(0, 0, 0, 0);
		this.defaultBgColor = new Cesium.Color(1, 1, 1);
		this._definitionChanged = new Cesium.Event();
		this._color = undefined;
		this._colorSubscription = undefined;
		this.color = Cesium.defaultValue(options.color, this.defaultColor); //颜色
		this.url = Cesium.defaultValue(options.url, undefined); //背景图片颜色
		if (!this.url) return;
		this.axisY = Boolean(options.axisY);
		this.bgUrl = Cesium.defaultValue(options.bgUrl, undefined); //背景图片颜色
		this.bgColor = Cesium.defaultValue(options.bgColor, this.defaultBgColor); //背景图片颜色
		this._duration = options.duration || 1000; //时长
		var _material = this.getImageMaterial(this.url, this.bgUrl, options.repeat, Boolean(options.axisY), this
		.bgColor);
		this._materialType = _material.type; //材质类型
		this._materialImage = _material.image; //材质图片
		this._time = undefined;
	}
	get color() {
		return this._color;
	}
	set color(color) {
		this._color = color;
	}
	getType(time) {
		return this._materialType;
	}
	getValue(time, result) {
		if (!Cesium.defined(result)) {
			result = {};
		}
		result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, this.defaultColor, result.color);
		result.image = this._materialImage;
		if (this._time === undefined) {
			this._time = new Date().getTime();
		}
		result.time = (new Date().getTime() - this._time) / this._duration;
		return result;
	}
	equals(other) {
		return this === other || //
			other instanceof LineFlowMaterial && Cesium.Property.equals(this._color, other._color);
	}
	isConstant() {
		return false;
	}
	definitionChanged() {
		return this._definitionChanged;
	}
	getImageMaterial(imgurl, bgUrl, repeat, axisY, bgColor){
		cacheIdx++;
		var typeName = nameEx + cacheIdx + "Type";
		var imageName = nameEx + cacheIdx + "Image";
		Cesium.Material[typeName] = typeName;
		Cesium.Material[imageName] = imgurl;
		if (bgUrl) {
			//存在2张url的，用叠加融合的效果
			Cesium.Material._materialCache.addMaterial(Cesium.Material[typeName], {
				fabric: {
					type: Cesium.Material.PolylineArrowLinkType,
					uniforms: {
						color: new Cesium.Color(1, 0, 0, 1.0),
						image: Cesium.Material[imageName],
						time: 0,
						repeat: repeat || new Cesium.Cartesian2(1.0, 1.0),
						axisY: axisY,
						image2: bgUrl,
						bgColor: bgColor
					},
					source: "//2个图片的叠加融合\r\nczm_material czm_getMaterial(czm_materialInput materialInput)\r\n{\r\nczm_material material = czm_getDefaultMaterial(materialInput);\r\nvec2 st = repeat * materialInput.st;\r\nvec4 colorImage = texture2D(image, vec2(fract((axisY?st.t:st.s) - time), st.t));\r\nif(color.a == 0.0)\r\n{\r\n    material.alpha = colorImage.a;\r\n    material.diffuse = colorImage.rgb; \r\n}\r\nelse\r\n{\r\n    material.alpha = colorImage.a * color.a;\r\n    material.diffuse = max(color.rgb * material.alpha * 3.0, color.rgb); \r\n}\r\nvec4 colorBG = texture2D(image2,materialInput.st);\r\nif(colorBG.a>0.5){\r\n    material.diffuse = bgColor.rgb;\r\n}\r\nreturn material;\r\n}"
				},
				translucent: function translucent() {
					return true;
				}
			});
		} else {
			Cesium.Material._materialCache.addMaterial(Cesium.Material[typeName], {
				fabric: {
					type: typeName,
					uniforms: {
						color: new Cesium.Color(1, 0, 0, 1.0),
						image: Cesium.Material[imageName],
						time: 0,
						repeat: repeat || new Cesium.Cartesian2(1.0, 1.0),
						axisY: axisY
					},
					source:"czm_material czm_getMaterial(czm_materialInput materialInput)\r\n{\r\n    czm_material material = czm_getDefaultMaterial(materialInput);\r\n    vec2 st = repeat * materialInput.st;\r\n    vec4 colorImage = texture2D(image, vec2(fract((axisY?st.t:st.s) - time), st.t));\r\n    if(color.a == 0.0)\r\n    {\r\n        material.alpha = colorImage.a;\r\n        material.diffuse = colorImage.rgb; \r\n    }\r\n    else\r\n    {\r\n        material.alpha = colorImage.a * color.a;\r\n        material.diffuse = max(color.rgb * material.alpha * 3.0, color.rgb); \r\n    }\r\n    return material;\r\n}"
				},
				translucent: function translucent() {
					return true;
				}
			});
		}
		return {
			type: Cesium.Material[typeName],
			image: Cesium.Material[imageName]
		};		
	}
}

export {
	LineFlowMaterial
};


